package com.choudou5.nlp.util;

import com.alibaba.fastjson.JSONArray;
import com.choudou5.solr.util.JsonUtil;
import com.choudou5.solr.util.LogDeBugUtil;
import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.bean.WordBean;
import com.choudou5.solr.util.http.HttpUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @name：PullWordUtil 说明
 * @author：xuhaowen
 * @date：2018-09-01
 * @link http://pullword.com
 */
public class PullWordUtil {

    public static void main(String[] args) {
        getTextAnalysis("波尔多红酒怎么样");
    }

    /**
     * 文本分析 暂无限制
     * @param text 500字以内
     * http://pullword.com
     */
    public static Set<WordBean> getTextAnalysis(String text) {
        String resp =  HttpUtils.get("http://api.pullword.com/get.php?source="+ StrUtil.encode(text, "UTF-8")+"&param1=0&param2=0&json=1");
//        System.out.println(resp);
        if(StrUtil.isBlank(resp))
            return null;
        try {
            JSONArray array = JsonUtil.toJsonArray(resp);
            if(array != null && array.size() > 0){
                Set<WordBean> list = new HashSet<>();
                for (int i = 0; i < array.size(); i++) {
                    list.add(new WordBean(array.getJSONObject(i).getString("t"), "none"));
                }
    //            LogDeBugUtil.debug(list);
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
